#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017-11-10 15:03
# @Author  : zejin
# @File    : readmysql.py

import pymysql


connection = pymysql.connect(host='localhost',user='root',password='root',db='wikiurl',charset='utf8mb4')


try:
    with connection.cursor() as cursor:
        sql = "select `urlname`,`urlhref` from `urls` where `id` is not null"
        # 为什么结果是1   sql = "select count(`urlname`) from `urls` where `id` is not null"
        # 返回总条数
        count = cursor.execute(sql)
        print(count)

        # 获取数据
        result = cursor.fetchall()
        print(result)

        # 如果已经fetch过下次fetch就是null数据了
        result2 = cursor.fetchmany(size=3)
        print(result2)
finally:
    connection.close()



